package com.kalaqia.base.ui;

import android.content.Context;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author kalaqia 421407393@qq.com
 * @since 0.0.5
 */
public class TitleToolbar extends Toolbar {

    //<editor-fold desc="标题类型">

    public static final int SCENE_HIDDEN = -1;
    public static final int SCENE_CENTER = 0;
    public static final int SCENE_MATERIAL = 1;


    @IntDef({SCENE_HIDDEN, SCENE_CENTER, SCENE_MATERIAL})
    @Retention(RetentionPolicy.SOURCE)
    @interface TextScene {
    }

    //</editor-fold>

    @TextScene
    private int scene;
    private AppCompatTextView tvTitle;
    private AppCompatTextView tvBack;

    public TitleToolbar(Context context) {
        this(context, null);
    }

    public TitleToolbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.include_toolbar1, this, true);
        tvBack = findViewById(R.id.id_base_toolbar_backup_tv);
    }

    public void initTitleScene(@TextScene int scene) {
        this.scene = scene;
        if (scene == SCENE_CENTER) {
            tvTitle = findViewById(R.id.id_base_toolbar_center_tv);
        }
    }

    @Override
    public void setTitle(int resId) {
        if (scene == SCENE_CENTER && tvTitle != null) {
            tvTitle.setText(resId);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        if (scene == SCENE_CENTER && tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    public void setDisplayShowHomeEnabled(boolean b) {
        tvBack.setVisibility(b ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setNavigationOnClickListener(OnClickListener listener) {
        if (tvBack != null) {
            tvBack.setOnClickListener(listener);
        }
    }
}
